﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Access.PowerTools.Utilities;
using Access.PowerTools.Database.BackUp.Plug_In;
using Access.PowerTools.AbstractionLayer;
using Access.PowerTools.Database.Restore.Plug_In;
using Access.PowerTools.GUI;

namespace Access.PowerTools.TestWinForm
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();

            if (Access.PowerTools.Settings.Globals.SimulationModeOn)
            {
                Access.PowerTools.Settings.Globals.HostApplication =
                    new Access.PowerTools.Simulation.Engine.SimulationCommunicationBridge();
            }

        }

        private void cmdBackUp_Click(object sender, EventArgs e)
        {
            txtLog.Text = "";
            System.Threading.Thread.Sleep(100);   

            string operationName = "Backup";
            started(operationName);

            string backupFolder =
                Access.PowerTools.Settings.Globals.ExportFilesFolder;

            FilesAndFolders.ClearFolder(backupFolder);

            AccessApplication app = new AccessApplication();

            BackUpAgent a = new BackUpAgent(app);
            
            a.ReportProgressStatus += new EventHandler<RunProgressEventArgs>(ReportProgressStatus);
            
            a.BackUpDatabase(backupFolder);

            toolStripProgressBar1.Value = 0;
            finished(operationName);
        }

        private void ReportProgressStatus(object sender, RunProgressEventArgs e)
        {
            object[] args = { sender, e };
            this.Invoke(new ReportProgressStatusDelegate(ReportProgressStatusInvoke), args);
        }

        private delegate void ReportProgressStatusDelegate(object sender, RunProgressEventArgs e);
        private void ReportProgressStatusInvoke(object sender, RunProgressEventArgs e)
        {
            toolStripProgressBar1.Minimum = 0;
            toolStripProgressBar1.Maximum = e.MaxCountValue;
            toolStripProgressBar1.Value = e.CurrentCountValue;

            txtLog.Text =  string.Format("{0:HH:mm:ss.fff}: ", DateTime.Now) +  
                           (e.Message + System.Environment.NewLine +
                           txtLog.Text);

            System.Threading.Thread.Sleep(10);   
        }

        private void started(string operationName)
        {
            RunProgressEventArgs e = new RunProgressEventArgs();
            e.CurrentCountValue = 0;
            e.MaxCountValue = 0;
            e.Message = operationName + " started.";
            ReportProgressStatus(this, e);
        }
        private void finished(string operationName)
        {
            RunProgressEventArgs e = new RunProgressEventArgs();
            e.CurrentCountValue = 0;
            e.MaxCountValue = 0;
            e.Message = operationName + " finished.";
            ReportProgressStatus(this, e);
        }

        private void cmdRestore_Click(object sender, EventArgs e)
        {
            txtLog.Text = "";
            System.Threading.Thread.Sleep(100);   

            string operationName = "Restore";
            started(operationName);
 
            string backupFolder =
                Access.PowerTools.Settings.Globals.ExportFilesFolder;

            string restoreFolder =
                Access.PowerTools.Settings.Globals.RestoredFilesFolder;

            FilesAndFolders.ClearFolder(restoreFolder);


            AccessApplication app = new AccessApplication();

            AccessDatabase database = app.CurrentDatabase;

            RestoreAgent a = new RestoreAgent(app);

            a.ReportProgressStatus += new EventHandler<RunProgressEventArgs>(ReportProgressStatus);    
            a.RestoreDatabase(backupFolder);

            finished(operationName);

        }

        private void cmdClearTestFolders_Click(object sender, EventArgs e)
        {
            string backupFolder =
                Access.PowerTools.Settings.Globals.ExportFilesFolder;
            FilesAndFolders.ClearFolder(backupFolder);

            string restoreFolder =
                Access.PowerTools.Settings.Globals.RestoredFilesFolder;
            FilesAndFolders.ClearFolder(restoreFolder);

            txtLog.Text = "Cleared: " +
                 System.Environment.NewLine +  
                 backupFolder +
                 System.Environment.NewLine +  
                 restoreFolder;   

        }

        private void cmdAbout_Click(object sender, EventArgs e)
        {
            Access.PowerTools.Settings.Globals.ShowAboutBox();    
        }

        private void cmdAddInRestore_Click(object sender, EventArgs e)
        {
            BackupRestoreForm form = new BackupRestoreForm();

            bool backUp = false;
            AccessApplication application = new AccessApplication();

            object agent =
                new Access.PowerTools.Database.Restore.Plug_In.RestoreAgent(application);

            string archiveFolder = Access.PowerTools.Settings.Globals.ExportFilesFolder;

            form.Init(backUp, agent, archiveFolder);

            form.Show();  

        }

        private void cmdAddInBackUp_Click(object sender, EventArgs e)
        {
            BackupRestoreForm form = new BackupRestoreForm();

            bool backUp = true;

            AccessApplication application = new AccessApplication();

            object agent =
                new Access.PowerTools.Database.BackUp.Plug_In.BackUpAgent(application);

            string archiveFolder = Access.PowerTools.Settings.Globals.ExportFilesFolder;

            form.Init(backUp, agent, archiveFolder);

            form.Show();  

        }
    }
}
